mc.game.inventory = function(){};

mc.game.inventory.show = function(){
	mc.game.army.show_for(mc.game.inventory);
}

mc.game.inventory.leader_click = function(event){
	var entry = mc.game.army.leader_select(this);
	var to1 = mc.cmd.get_right_inner(1).empty();
	var to2 = mc.cmd.get_right_inner(2).empty();
	
	mc.game.load_start();
	$.when(
		mc.game.inventory.show_item(entry,to1,to2)
	).then(mc.game.load_end);
	
	if(event){
		event.stopImmediatePropagation();
	}
}

mc.game.inventory.show_item = function(entry,to1,to2){
	var source = mc.leader_list.find_source();
	if(source.entry == entry){
		return $.when(
				mc.leader_list.ask_item(entry)
			).then(function(){
				mc.game.inventory.draw_source(entry,to1);
			});
	}
	return $.when(
			mc.leader_list.ask_item(entry),
			mc.leader_list.ask_item(source.entry)
		).then(function(){
			mc.game.inventory.draw_item(entry,to1,to2);
		});
}
mc.game.inventory.draw_source = function(entry,to1){
	var o = $.tmpl("inventory_item_source_has",mc.leader_list.inventory(entry,true)).appendTo(to1);
	var i = o.find(">div");
	mc.game.calc_width(i);
	o.jScrollPane(scroll_pane);
}
mc.game.inventory.draw_item = function(entry,to1,to2){
	var data = mc.leader_list.inventory(entry);
	var o = $.tmpl("inventory_item_has",data).appendTo(to1);
	var i = o.find(">div");
	mc.game.calc_width(i);
	o.jScrollPane(scroll_pane);
	o = $.tmpl("inventory_item_can",data).appendTo(to2);
	i = o.find(">div");
	mc.game.calc_width(i);
	o.jScrollPane(scroll_pane);
}
mc.game.inventory.redraw_item = function(entry){
	var to1 = mc.cmd.get_right_inner(1).empty();
	var to2 = mc.cmd.get_right_inner(2).empty();
	mc.game.inventory.show_item(entry,to1,to2);
}

mc.game.inventory.item_move = function(show,source,target,entry){
	mc.game.load_start();
	$.when(
		mc.request(
			"game",
			{
				source : source,
				target: target,
				entry: entry,
				cmd: "items.move"
			},
			function(data){
				mc.leader_list.set_items(this.source, data.source);
				mc.leader_list.set_items(this.target, data.target);
				mc.game.inventory.redraw_item(this.show);
			},
			{
				source : source,
				target: target,
				show: show
			}
		)
	).then(mc.game.load_end);
}
mc.game.inventory.toleader = function(source,target,entry){
	this.item_move(target,source,target,entry);
}
mc.game.inventory.tosource = function(source,target,entry){
	this.item_move(target,target,source,entry);
}
mc.game.inventory.addsource = function(leader,proto){
	mc.request(
		"game",
		{
			leader: leader,
			proto: proto,
			cmd: "items.add"
		},
		function(data){
			mc.leader_list.set_items(this.leader, data);
			mc.game.inventory.redraw_item(this.leader);
		},
		{
			leader: leader
		}
	);
}
